/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年6月1日
 */
package com.massyframework.beanskin.runtime.framework.installing.plugin;

import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.massyframework.beanskin.ModuleIdentifier;
import com.massyframework.beanskin.ModuleRuntimeException;
import com.massyframework.beanskin.RequireLibraryDescriptor;
import com.massyframework.beanskin.assembly.Assembly;
import com.massyframework.beanskin.plugin.PluginBuilder;
import com.massyframework.beanskin.runtime.framework.installing.Expression;
import com.massyframework.beanskin.runtime.framework.installing.InitParamsModuleParser;
import com.massyframework.beanskin.util.exceptioncollect.ExceptionCollectUtils;
import com.massyframework.beanskin.util.localstore.LocalStore;
import com.massyframework.beanskin.util.variables.StringVariableReplacer;

/**
 * 插件xml解析器
 * @author huangkh
 *
 */
public class PluginXmlParser extends InitParamsModuleParser<PluginBuilder> {
	
	private static final String ACTIVATOR = "activator";
	
	/**
	 * 构造方法
	 * @param kernel {@link Assembly},内核装配件
	 * @param localStore {@link LocalStore},本地存储
	 */
	public PluginXmlParser(Assembly kernel, LocalStore localStore) {
		super(kernel, localStore);
	}

	

	@Override
	protected void doParseNodeAfterInitParams(Document doc, Expression<String> expression, StringVariableReplacer replacer, PluginBuilder builder) {
		super.doParseNodeAfterInitParams(doc, expression, replacer, builder);
		try {
			NodeList list = doc.getElementsByTagName(ACTIVATOR);
			Node node = list.item(0);
			builder.activatorClassName(this.getTextContent(node));
		}catch(Exception e) {
			ModuleRuntimeException newException = 
					new ModuleRuntimeException(this.getModuleIdentifier(), e);
			ExceptionCollectUtils.addException(newException);
			throw newException;
		}
	}
	
	@Override
	protected void setDecryption(String publicKey, String certificateName, 
			String certificateData, List<String> classNames, List<String> scriptFileNames, PluginBuilder builder) {
		builder.decryption(publicKey, certificateName, certificateData, classNames, scriptFileNames);
	}

	@Override
	protected boolean setParamter(String name, String value, PluginBuilder builder) {
		AtomicBoolean result = new AtomicBoolean(true);
		builder.setParameter(name, value, rst -> result.set(rst));
		return result.get();
	}
	
	@Override
	protected void addRequriedLibrary(RequireLibraryDescriptor descriptor, PluginBuilder builder) {
		builder.addRequireLibrary(descriptor);
	}

	@Override
	protected void setModuleIdentifier(ModuleIdentifier identifier, PluginBuilder builder) {
		builder.identifier(identifier);
	}

	@Override
	protected void setModuleName(String moduleName, PluginBuilder builder) {
		builder.name(moduleName);
	}

	@Override
	protected void setModuleDescription(String description, PluginBuilder builder) {
		builder.description(description);
	}
	
	@Override
	protected void setTags(Set<String> tags, PluginBuilder builder) {
		builder.addTags(tags);
	}
	
	@Override
	protected void setDeveloper(String shortName, String name, String homePage, PluginBuilder builder) {
		builder.developer(shortName, name, homePage);
	}

	@Override
	protected String getResouceNameWithXmlSchemaFile() {
		return "com/massyframework/beanskin/config/plugin-2.0.0.xsd";
	}

	@Override
	protected Class<PluginBuilder> getBuilderType() {
		return PluginBuilder.class;
	}

}
